package com.atguigu.bigdata.spark

import org.apache.spark.{SparkConf, SparkContext}


object WordCount {
  def main(args: Array[String]): Unit = {
    //local 模式
    //创建SparkConf
    //s设定spark计算框架的运行环境
    val config: SparkConf = new SparkConf().setMaster("local[*]").setAppName("wordCount")
    //创建Spark上下文环境
    val sc = new SparkContext(config)
    //读取文件
    var lines = sc.textFile("in")
    //将一行一行数据分解为一个一个单词
    val words = lines.flatMap(_.split(" "))
    //为了方便统计，将单词数据进行结构的转换
    val wordToOne = words.map((_,1))
//   对转换后的数据进行分组聚合
    val wordToSum = wordToOne.reduceByKey(_+_)
    //result
    val result = wordToSum.collect()
    //println(result)
    result.foreach(println)

  }
}
